package com.tubang.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 返回前端的数据包装
 */
public class ResultData {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	/*
	 * 调用结果
	 */
	private boolean success;

	/*
	 * 错误或成功代码
	 */
	private int code;

	/*
	 * 服务器端返回消息
	 */
	private String msg;

	/**
	 * 1:成功 0:失败
	 */
	private int status;

	private String language;
	/**
	 * 总行数
	 */
	private Integer totalCount;
	/*
	 * 数据对象
	 */
	private Object data;
//	/**
//	 * 页码
//	 */
//	private int page;
//	/**
//	 * 每页显示的条数
//	 */
//	private int pageNo;
//	/**
//	 * 总页数
//	 */
//	private int totalPageCount;
	public Integer getTotalCount() {
		return totalCount;
	}

	public ResultData setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
		return this;
	}

//	public int getTotalPageCount() {
//		return totalPageCount;
//	}
//
//	public ResultData setTotalPageCount(int totalPageCount) {
//		this.totalPageCount = totalPageCount;
//		return this;
//	}
//
//	public int getPageNo() {
//		return pageNo;
//	}
//
//	public ResultData setPageNo(int pageNo) {
//		this.pageNo = pageNo;
//		return this;
//	}
//	public int getPage() {
//		return page;
//	}
//
//	public ResultData setPage(int page) {
//		this.page = page;
//		return this;
//	}

	public ResultData() {
	}

	public boolean success() {
		return success;
	}

	public int getCode() {
		return code;
	}

	public ResultData setCode(int code) {
		this.code = code;
		return this;
	}

	public boolean isSuccess() {
		return success;
	}

	public ResultData setSuccess(boolean success) {
		this.success = success;
		return this;
	}

	/**
	 * 分解code返回提示信息 未设置code时，
	 * 如果有msg信息直接返回msg，
	 * 否则默认返回500错误并打印日志提示开发者
	 */
	public String getMsg() {
		if (code <= 0) {
			if (msg != null && !"".equals(msg)) {
				return msg;
			}
			code = MsgCode.SERVER_ERROR;
			logger.warn("Msg Code is Invalid !");
		}
		language = "zh_CN";
		return msg;
	}

	public ResultData setMsg(String msg) {
		this.msg = msg;
		return this;
	}

	public void setLanguage(String language) {
		this.language = language;
	}



	public static ResultData result(boolean result) {
		return new ResultData().setSuccess(result).setStatus(result ? 1 : 0)
				.setCode(result ? MsgCode.SUCCESS : MsgCode.SERVER_ERROR)
				.setMsg(result ? MsgCode.DEFAULT_SUCCESS_MSG : MsgCode.SERVER_ERROR_MSG);
	}

	public String getLanguage() {
		return language;
	}

	public int getStatus() {
		return status;
	}

	public ResultData setStatus(int status) {
		this.status = status;
		return this;
	}

	public Object getData() {
		return data;
	}

	public ResultData setData(Object data) {
		this.data = data;
		return this;
	}

	@Override
	public String toString() {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("ResultData [success=");
		stringBuilder.append(success);
		stringBuilder.append(", code=");
		stringBuilder.append(code);
		stringBuilder.append(", msg=");
		stringBuilder.append(msg);
		stringBuilder.append(", status=");
		stringBuilder.append(status);
		stringBuilder.append(", language=");
		stringBuilder.append(language);
//		stringBuilder.append(", page=");
//		stringBuilder.append(page);
//		stringBuilder.append(", pageNo=");
//		stringBuilder.append(pageNo);
//		stringBuilder.append(", totalPageCount=");
//		stringBuilder.append(totalPageCount);
		stringBuilder.append(", totalCount=");
		stringBuilder.append(totalCount);
		stringBuilder.append(", data=");
		stringBuilder.append(data);
		stringBuilder.append("]");
		return stringBuilder.toString();
	}

	public static void main(String[] args) {
		ResultData resultData = ResultData.result(true).setData("ermao").setMsg("成功了");
		String msg2 = resultData.getMsg();
		System.out.println("resultData: " + resultData + msg2);
		System.out.println(ResultData.result(false));
	}
}
